<?php
// include constants
include "./includes/vars.php";
include "./includes/containers.php";
include "newvars.php";
$pathToRoot = "./"; 		// or "";

?>

<?php
include "header.php";
?>


<?php
// content php code goes here

$link = mysql_connect($dbhost, $dbuser, $dbpass)
or Die("Cannot connect to database server");

mysql_select_db($dbname)
  or Die("Database not found.");


function getDwonloadBookLink1($link){
$pathToRoot = "./"; 		// or "";

	return '<a href="' . $pathToRoot . 'index.php?act=download&BID=' . $link . '" class="link_download">Download this book</a>';
	//return '<A HREF="index.php?act=download&BID=' . $link . '"><img src="images/downloadThisBook.gif" BORDER=0></A>';
}

//added by walid.elswaify on 2 May 2008 to support new UI
function getBookImageLink($imageUrl, $altMsg) {
return '<img src="' . $imageUrl . '" alt="' . $altMsg . '" width="45" height="70"/>';
}

// content management

if (isset($_GET['bkID'])){

//view chapter content    

          // get the book's name
          $sql = "SELECT B_NAME, B_DOWNLOAD_COUNT, B_IMAGE_URL FROM books WHERE B_ID = " . $_GET['bkID'];
          if ($result = mysql_query($sql)){

            $row = mysql_fetch_row($result);
            $bookname = $row[0];			
			$bookimage = '<img border="0" width="100" height="150" src="' . $websitename . $row[2] . '"><br>';			
			$bookdownload = getDwonloadBookLink1($_GET['bkID']);
			$bookImageLink = getBookImageLink($websitename . $row[2] , $bookname);
		  }
          else{
              $bookname = "";
          }


        //print the chapter in the inline frame
        $sql = "SELECT CHAP_URL  FROM chapters WHERE CHAP_ID = " . $_GET['chapter'] . " AND CHAP_BK_ID = " . $_GET['bkID'];
        if ($result = mysql_query($sql)){
        
            $row = mysql_fetch_row($result);
            
            // check the first letter
            $firstletter = substr($row[0], 0, 1);
            if ($firstletter == "/"){
                $urllength = strlen($row[0]);
                $chapterurl = substr($row[0], 1, $urllength);
            }
            else{
                $chapterurl = $row[0];
            }
           
                                      // get the file and extract the body --------------------------------------------------
                                      $filename = $pathToRoot . $chapterurl; //modified by walid.elswaify

                                      $filetext = implode("", file($filename));

                                      $filetext = eregi_replace ("<body", "IBREPLACEMENT", $filetext);
                                      $filetext = eregi_replace ("</body", "IBREPLACEMENT", $filetext);

                                      $fileparts = explode("IBREPLACEMENT", $filetext);




                                      $fileparts2 = split (">", $fileparts[1], 2);



                                      $contenttable = $fileparts2[1];
            
            
            
            
            
            
            
            
            
            
            //$contenttable = '<iframe width="' . $iframewidth . '" height="' . $iframeheight . '" src="' . $websitename . $row[0] . '"></iframe>';

        
        }
        else{
            $content = "Could not retrieve this chapter";
        }
	
	
	//prepare the next, previous and content links
        // content
        $tblofcontentlnk = '<A HREF="view.php?bkID=' . $_GET['bkID'] . '"><img src="' . $websitename . '/images/' . $contentimage . '" border="0"></A>';
		$myTableOfContentLink = '<A HREF="' . $pathToRoot . 'chapters.php?bkID=' . $_GET['bkID'] . '">Contents</A>';
        //next link

        $nextlink = "";
        $sql = "SELECT CHAP_ID, CHAP_SEQUENCE FROM chapters WHERE CHAP_BK_ID = " . $_GET['bkID'] . " ORDER BY CHAP_SEQUENCE";
        if ($result = mysql_query($sql)){
            $nextfound = "no";
            $chapterfound = "no";
            while ($row = mysql_fetch_row($result)){


                if ($chapterfound == "yes"){
                    $nextfound = "yes";
                    $nextchapterID = $row[0];
                    $chapterfound = "no";
                }
                
                if ($row[0] == $_GET['chapter']){
                    $chapterfound = "yes";
                }


            }
            
            if ($nextfound == "yes"){
                $nextlink = '<A HREF="view.php?bkID=' . $_GET['bkID'] . '&chapter=' . $nextchapterID . '"><img src="' . $websitename . '/images/ib_next.gif" border="0"></A>';
				$myNextLink = '<a title="Next" href="' . 'test.php?bkID=' . $_GET['bkID'] . '&chapter=' . $nextchapterID . '">&raquo;</a>';
            }
        }
        
        
        
        // previous link
        $prevlink = "";
        if ($result = mysql_query($sql)){
            $prevfound = "no";
            $stop = "no";
            while ($row = mysql_fetch_row($result)){



                if ($row[0] == $_GET['chapter']){
                    $stop = "yes";
                }

                if ($row[0] != $_GET['chapter'] && $stop != "yes"){
                    $prevfound = "yes";
                    $prevID = $row[0];
                }

            
            }
            
            if ($prevfound == "yes"){
                $prevlink = '<A HREF="view.php?bkID=' . $_GET['bkID'] . '&chapter=' . $prevID . '"><img src="' . $websitename . '/images/ib_previous.gif" border="0"></A>';
				$myPrevLink = '<a title="Previous" href="' . 'test.php?bkID=' . $_GET['bkID'] . '&chapter=' . $prevID . '">&laquo;</a>';

            }

        }

        $navigation = $prevlink . ' ' . $tblofcontentlnk . ' ' . $nextlink ;
		$myNavigation = $myPrevLink . ' ' . $myTableOfContentLink . ' ' . $myNextLink ;
                   // print chapters list
          $sql = "SELECT CHAP_ID, CHAP_NAME FROM chapters WHERE CHAP_BK_ID = " . $_GET['bkID'] . " ORDER BY CHAP_SEQUENCE";

          if ($result = mysql_query($sql)){
              $chapterlist = "";
			  $chapterOptions = "";
              while ($row = mysql_fetch_row($result)){

                  $chapterlist .= '<a href="view.php?bkID=' . $_GET['bkID'] . '&chapter=' . $row[0] . '">' . $row[1] . '</a><BR>';
				  $chapterOptions .= '<option value="test.php?bkID=' . $_GET['bkID'] . '&chapter=' . $row[0] . '">' . $row[0] . '</option>';

              }           }
    
    //end of chapter content view
        $authsql ="SELECT BA_AUTH_ID FROM bk_auth WHERE BA_BOOK_ID = " . $_GET['bkID'];
    if ($authresult = mysql_query($authsql)){
        $authrow = mysql_fetch_row($authresult);
        $authsql2 ="SELECT * FROM authors WHERE A_ID = " . $authrow[0];
        if($authresult2 = mysql_query($authsql2)){
            $authrow2 = mysql_fetch_row($authresult2);
        }		if($authrow2[2]){
			$authorname .= 'By: <a href="' . $pathToRoot . 'view.php?authID=' . $authrow2[0] . '">' . $authrow2[1] . " " . $authrow2[2] . '</a><BR></P>';
		}    }      
   
    
    // replace the tags
    if (isset($_GET['chapter'])){
        $chapterview = str_replace("<!--NAVIGATION-->", $navigation, $chapterview);		$sidebar = str_replace("<!--CHAPTERLIST-->", $chapterlist, $sidebar);
    }
    $chapterview = str_replace("<!--CONTENT-->", $contenttable, $chapterview);
    $chapterview = str_replace("<!--BOOKNAME-->", $bookname, $chapterview);
    $content = $chapterview;
    $sidebar = str_replace("<!--BOOKIMAGE-->", $bookimage, $sidebar);    $sidebar = str_replace("<!--BOOKDOWNLOAD-->", $bookdownload, $sidebar);    $sidebar = str_replace("<!--AUTHORNAME-->", $authorname, $sidebar);
}






else{

$content ='
<script language="javascript">
<!--

document.location="index.php";


-->
</script>
';


}









// prepare and send final file
/*
commented by walid.elswaify
if (file_exists($templatefile)){
    $output = implode ("", file($templatefile));
    $output = str_replace("<!--CONTENT-->", $content, $output);    $output = str_replace("<!--SIDEBAR-->", $sidebar, $output);
    print $output;

    
}
else{
    print "ERROR";
}
*/

mysql_close ($link);

		
?>
<!-- content html code goes here-->
<td rowspan="2" class="content_center" valign="top">
            <div class="content_header">
                <div class="content_header_image float_left">
                    <?php print $bookImageLink; ?></div>
                <div class="content_header_details">
                    <h2><?php print $bookname; ?></h2>
                    <?php print $authorname; ?>
                    <p>
                        <?php print $bookdownload; ?>
                    </p>
                </div>
            </div>
            <div class="content_pagination">
                <div class="FloatRight">Chapter:
                    <label>
                        <select name="select" onchange="location.href=this.value">
                            <option selected>Please select</option>
							<?php print $chapterOptions;?>
                        </select>
                    </label>
				
						<?php print $myNavigation;?>
						</div>
            </div>
            <div class="content_body">
		
				
				<?php print $contenttable;?>
				
				
            </div>
        </td>
<!-- end of content html code-->



<?php
include "footer.php";
?>
